#ifndef SCRIPTINTERFACE_H
#define SCRIPTINTERFACE_H

#include "Framework/Prerequisites.h"
#include "Framework/ScriptParser/ParameterDictionary.h"

namespace tyro
{
	namespace Framework
	{
		typedef map<String, ParameterDictionary>::type ParamDictionaryMap;


		class ScriptInterface
		{
		private:

			// Dictionary of parameters
			static ParamDictionaryMap dictionaryMap;

			// Dictionary name
			String					dictionaryName;
			ParameterDictionary*	dictionary;

		protected:

			bool CreateDictionary(const String& name)
			{
				ParamDictionaryMap::iterator itr = dictionaryMap.find(name);
				if (itr == dictionaryMap.end())
				{
					dictionary = &dictionaryMap.insert( std::make_pair( name, ParameterDictionary())).first->second;
					dictionaryName = name;
					return true;
				} 
				else
				{
					dictionary = &itr->second;
					dictionaryName = name;
					return false;
				}
			}

		public:

			ScriptInterface() : dictionary(NULL){}
			virtual ~ScriptInterface(){}
		
			ParameterDictionary* GetParameterDictionary(void)
			{
				return dictionary;
			}

			const ParameterDictionary* GetParameterDictionary(void) const
			{
				return dictionary;
			}

			const ParameterList& GetParameters(void) const;
			virtual bool SetParameter(const String& name, const String& value);
			virtual void SetParameterList(const NameValuePairList& paramList);

			virtual String GetParameter(const String& name) const
			{
				// Get the dictionary
				const ParameterDictionary* dict = GetParameterDictionary();

				if (dict)
				{
					// Retrieve the command object
					const ParameterCommand* cmd = dict->GetParameterCommand(name);

					if (cmd)
					{
						return cmd->Get(this);
					}
				}

				// Default
				return "";
			}


			virtual void CopyParametersTo(ScriptInterface* dest) const
			{
				// Get dictionary
				const ParameterDictionary* dict = GetParameterDictionary();

				if (dict)
				{
					// Iterate through own parameters
					ParameterList::const_iterator i;

					for (i = dict->parametersDefs.begin(); 
						i != dict->parametersDefs.end(); ++i)
					{
						dest->SetParameter(i->name, GetParameter(i->name));
					}
				}


			}

			static void CleanupDictionary();
		
		};

	}
}


#endif